// Copyright 2021 Guoyoko. All Rights Reserved.

#include "Slate/SSWPropertyTextWidget.h"
#include "Widgets/Input/SEditableTextBox.h"


void SSWPropertyTextWidget::Construct(const FArguments& InArgs, FSWBaseProperty* InBaseProperty)
{
	SSWPropertyRowBaseWidget::Construct(SSWPropertyRowBaseWidget::FArguments().NodeName(InArgs._NodeName), InBaseProperty); 

	TSharedRef<SBox> PropertyLeftRow =
		SNew(SBox)
		[
			SNew(STextBlock)
			.Text(FText::FromName(NodeName))
		];

	TSharedRef<SBox> PropertyRightRow =
		SNew(SBox)
		[
			SNew(SEditableTextBox)
			.SelectAllTextWhenFocused(true)
			.Text(this, &SSWPropertyTextWidget::GetEditableText)
			.OnTextCommitted(this, &SSWPropertyTextWidget::OnEditableTextCommited)
			.OnTextChanged(this, &SSWPropertyTextWidget::OnEditableTextCommited, ETextCommit::Default)
		];

	ChildSlot
	[
		SNew(SHorizontalBox)
		+ SHorizontalBox::Slot()
		[
			PropertyLeftRow
		]
		+ SHorizontalBox::Slot()
		[
			PropertyRightRow
		]
	];
}

FText SSWPropertyTextWidget::GetEditableText() const
{
	return FText::FromString(ValueText);
}

void SSWPropertyTextWidget::OnEditableTextCommited(const FText& InText, ETextCommit::Type InCommitType)
{
	ValueText = InText.ToString();
}

